/**
 * @author : cyb1010
 * @date   : 2024-12-26 09:27:19
 * @file   : tree.cpp
 */
#include <bits/stdc++.h>
using namespace std;
#define fo(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define fi first
#define se second
#define ALL(v) (v).begin(), (v).end()
#define SZ(v) int(v.size())
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
void ckmx(int &x, int y) { x < y && (x = y); }
void ckmn(int &x, int y) { x > y && (x = y); }
const int N = 200010;
int __, n, q, c, ans[N], a[N], s[N];
vector<int> qry[N];
vector<int> g[N];

int sz[N], sztot, szrt, rt, rtfa;
bool vis[N];
void get_rt(int u, int fa) {
    sz[u] = 1;
    int mx = 0;
    for (int v : g[u])
        if (v != fa && !vis[v]) get_rt(v, u), sz[u] += sz[v], ckmx(mx, sz[v]);
    ckmx(mx, sztot - sz[u]);
    if (mx < szrt) szrt = mx, rt = u, rtfa = fa;
}

vector<int> U[1 << 7 | 10];
int buc[1 << 8 | 10][1 << 7 | 10];
void add(int x, int y) { // n log n times
    int u = x >> 7, v = x & 127;
    for (int i : U[v]) buc[u][i] += y;
}
int query(int x) { // q times
    int u = x >> 7, v = x & 127, res = buc[0][v];
    for (int i = u; i; i = (i - 1) & u) res += buc[i][v];
    return res;
}

void init(int p, int fa, int c) {
    c |= a[p], add(c, 1);
    vector<int> tmp;
    for (int t : qry[p])
        if ((s[t] & c) == c && p != rt)
            qry[rt].push_back(t);
        else
            tmp.push_back(t);
    qry[p].swap(tmp);
    for (int i : g[p])
        if (i != fa && !vis[i]) init(i, p, c);
}
void clr(int p, int fa, int c) {
    c |= a[p], add(c, -1);
    for (int i : g[p])
        if (i != fa && !vis[i]) clr(i, p, c);
}
void solve(int p) {
    sztot = szrt = sz[p], get_rt(p, 0), vis[p = rt] = 1;
    sz[rtfa] = sztot - sz[p], init(p, 0, 0);
    for (int id : qry[p]) ans[id] = query(s[id]);
    clr(p, 0, 0), vis[p] = 1;
    for (int i : g[p])
        if (!vis[i]) solve(i);
}

int main() {
    fo("tree");
    for (int i = 0; i < 128; i++) {
        U[0].push_back(i);
        for (int j = i; j; j = (j - 1) & i) U[j].push_back(i);
    }
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> q >> c;
    for (int i = 1; i <= n; i++) cin >> a[i], a[i] = 1 << a[i];
    for (int i = 2, x; i <= n; i++)
        cin >> x, g[i].push_back(x), g[x].push_back(i);
    for (int i = 1, x; i <= q; i++) cin >> s[i] >> x, qry[x].push_back(i);
    sz[1] = n, solve(1);
    for (int i = 1; i <= q; i++) cout << max(ans[i], 1) << '\n';
    cerr << clock() << '\n';
    return 0 ^ __ ^ 0;
}